const express = require('express')
const fs = require('fs')
const router = express.Router()
const request = require('request')

router.post('/guoji', (req, res) => {

    fs.readFile('res/newGuoji.json', (err, data) => {
        if (!err) {
            const list = JSON.parse(data).result.list
            let arr = []
            for (let i = 0; i < 20; i++) {
                arr.push(list[i])
            }
            res.send(arr)
        }
    })

})
router.post('/home', (req, res) => {

    fs.readFile('res/toutiao.json', (err, data) => {

        if (req.body.id) {
            const list = JSON.parse(data).result.list[req.body.id]
            res.send(list)
        } else {

            let arr = []
            let obj = {}

            for (let i = req.body.length; i < req.body.length * 1 + 10; i++) {

                obj = JSON.parse(data).result.list[i]
                obj.id = i

                arr.push(obj)
            }

            res.send(arr)
        }

    })

})

router.post('/hot', (req, res) => {

    fs.readFile('res/newGuoji.json', (err, data) => {
        if (req.body.id) {
            const list = JSON.parse(data).result.list[req.body.id]
            res.send(list)
        } else {
            let arr = []
            let obj = {}
            for (let i = req.body.length; i < req.body.length * 1 + 10; i++) {
                obj = JSON.parse(data).result.list[i]
                obj.id = i
                arr.push(obj)
            }

            res.send(arr)
        }
    })

})

router.post('/jiaoyu', (req, res) => {

    fs.readFile('res/jiaoyu.json', (err, data) => {
        if (req.body.id) {
            const list = JSON.parse(data).result.list[req.body.id]
            res.send(list)
        } else {
            let arr = []
            let obj = {}
            for (let i = req.body.length; i < req.body.length * 1 + 10; i++) {
                obj = JSON.parse(data).result.list[i]
                obj.id = i
                arr.push(obj)
            }

            res.send(arr)
        }
    })

})

router.post('/shehui', (req, res) => {

    fs.readFile('res/newJs.json', (err, data) => {
        if (req.body.id) {
            const list = JSON.parse(data).result.list[req.body.id]
            res.send(list)
        } else {
            let arr = []
            let obj = {}
            for (let i = req.body.length; i < req.body.length * 1 + 10; i++) {
                obj = JSON.parse(data).result.list[i]
                obj.id = i
                arr.push(obj)
            }

            res.send(arr)
        }
    })

})
router.post('/yule', (req, res) => {

    fs.readFile('res/newsTy.json', (err, data) => {
        if (req.body.id) {
            const list = JSON.parse(data).result.list[req.body.id]
            res.send(list)
        } else {
            let arr = []
            let obj = {}
            for (let i = req.body.length; i < req.body.length * 1 + 10; i++) {
                obj = JSON.parse(data).result.list[i]
                obj.id = i
                arr.push(obj)
            }

            res.send(arr)
        }
    })

})

router.post('/keji', (req, res) => {

    fs.readFile('res/keji.json', (err, data) => {
        if (req.body.id) {
            const list = JSON.parse(data).result.list[req.body.id]
            res.send(list)
        } else {
            let arr = []
            let obj = {}
            for (let i = req.body.length; i < req.body.length * 1 + 10; i++) {
                obj = JSON.parse(data).result.list[i]
                obj.id = i
                arr.push(obj)
            }

            res.send(arr)
        }
    })

})

router.get('/live', (req, res) => {
    request('http://capi.douyucdn.cn/api/v1/live?limit=20&offset=0', (err, response, body) => {
        if (!err) {
            res.send(body)
        }
    })
})






module.exports = router